#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <QWidget>
#include <QSerialPort>
#include <QTimer>
#include <QString>
#include <QMainWindow>

#include "ui/secondwindow.h"
#include "ui/timer.h"
#include "ui/formdevinfo.h"
#include "ui/formdevlog.h"
#include "ui/serial.h"


#include <QLabel>
#include <QDialog>
#include <QTimer>


QT_BEGIN_NAMESPACE
namespace Ui { class example; }
QT_END_NAMESPACE

class example : public QWidget
{
    Q_OBJECT

public:
    example(QWidget *parent = nullptr);
    ~example();


    enum class TestStatus {
        Running,  // 进行中（黄色）
        Success,  // 成功（绿色）
        Failed    // 失败（红色）
    };

    QSerialPort *serialPort;

private slots:
    void on_openBt_clicked();
    void on_closeBt_clicked();
    void serialPortReadyRead_Solt();
    void on_sendBt_clicked();
    void on_clearBt_clicked();
    void on_refreshBt_clicked();
    void on_clearLogBtn_clicked();
    void on_saveLogBtn_clicked();
    void on_loadLogBtn_clicked();

    void sendCtrlCAndDelay(int ms = 800);

    // 测试功能
    void on_init_test_clicked();
    void on_ddr_test_clicked();
    void on_emmc_test_clicked();
    void on_rtc_test_clicked();
    void on_wifi_test_clicked();
    void on_output1_test_clicked();
    void on_output2_test_clicked();
    void on_input1_test_clicked();
    void on_input2_test_clicked();
    void on_test1_422_clicked();
    void onUserWaitTimeout();
    void on_test2_422_clicked();  // 新增 422-2 测试按钮槽函数
    void onUserWaitTimeout1();

    void on_mAoutput1_test_clicked();
    void on_mAoutput2_test_clicked();

    void on_mAin1_test_clicked();
    void on_mAin2_test_clicked();


    // 新增 485 测试按钮槽函数
    void on_test1_485_clicked();
    void on_test2_485_clicked();
    void on_test3_485_clicked();
    void on_test4_485_clicked();

    // 485 倒计时超时处理
   void onUserWaitTimeout_485_1();
   void onUserWaitTimeout_485_2();
   void onUserWaitTimeout_485_3();
   void onUserWaitTimeout_485_4();

   //网口测试
   void on_test1_eth_clicked();
   void on_test2_eth_clicked();
   void on_test3_eth_clicked();
   void on_test4_eth_clicked();
   void on_test5_eth_clicked();

   //4g
   void on_test_4g_clicked();

   //usb
   void on_usb_test_clicked();

   void on_hdd_test_clicked();  // ← 新增

   void on_pb_test_clicked();  // ← 新增光口测试按钮槽
   void on_led_test_clicked();  // ← 新增

   //all
   void on_summaryBtn_clicked();
   void on_showReportBtn_clicked();


    // example.h（在 private slots 下添加）
    void on_switchBt_clicked();  // 新增槽函数
    void on_timerBt_clicked();  // 新增槽函数
    void on_devInfo_clicked();  //新增页面
    void on_devLog_clicked();   //新增页面
    void on_serialBt_clicked();

    void on_writeSnBtn_clicked();
    void on_writeSnBtn1_clicked();

private:
    Ui::example *ui;
    void appendLog(const QString &msg, bool success);
    void updateStatus(const QString &msg, TestStatus status);
    void updateTestButtonsStyle();  // 统一更新按钮样式
    void resetAllTestStates();  // 新增：重置所有测试状态

    void sendStringToSerialPort(const QString &portName, const QByteArray &data);
    void promptUserForResult(bool& successFlag, const QString& testName);


    // 测试状态标志与定时器
    bool m_waitingForRtcResult = false;
    bool m_waitingForDdrResult = false;
    bool m_waitingForEmmcResult = false;
    bool m_waitingForWifiResult = false;
    bool m_waitingForInitSuccess = false;
    bool m_waitingForOutput1Result = false;
    bool m_waitingForOutput2Result = false;
    bool m_waitingForInput1Result = false;
    bool m_waitingForInput2Result = false;
    bool m_waitingFor422_1Result = false;
    bool m_waitingFor422_2Result = false;

    // 485 测试状态标志
    bool m_waitingFor485_1Result = false;
    bool m_waitingFor485_2Result = false;
    bool m_waitingFor485_3Result = false;
    bool m_waitingFor485_4Result = false;

    // ===== 网口测试状态 =====
    bool m_waitingForEth1Result = false;
    bool m_waitingForEth2Result = false;
    bool m_waitingForEth3Result = false;
    bool m_waitingForEth4Result = false;
    bool m_waitingForEth5Result = false;

    // 4G 测试状态
    bool m_waitingFor4gResult = false;

    // USB 测试状态
    bool m_waitingForUsbResult = false;





    QTimer *m_rtcTimeoutTimer = nullptr;
    QTimer *m_ddrTimeoutTimer = nullptr;
    QTimer *m_emmcTimeoutTimer = nullptr;
    QTimer *m_wifiTimeoutTimer = nullptr;
    QTimer *m_output1TimeoutTimer = nullptr;
    QTimer *m_output2TimeoutTimer = nullptr;
    QTimer *m_input1TimeoutTimer = nullptr;
    QTimer *m_input2TimeoutTimer = nullptr;
    QTimer *m_422_1TimeoutTimer = nullptr;
    QTimer *m_422_2TimeoutTimer = nullptr;

    // 485 超时定时器
    QTimer *m_485_1TimeoutTimer = nullptr;
    QTimer *m_485_2TimeoutTimer = nullptr;
    QTimer *m_485_3TimeoutTimer = nullptr;
    QTimer *m_485_4TimeoutTimer = nullptr;

    QTimer *m_eth1TimeoutTimer = nullptr;
    QTimer *m_eth2TimeoutTimer = nullptr;
    QTimer *m_eth3TimeoutTimer = nullptr;
    QTimer *m_eth4TimeoutTimer = nullptr;
    QTimer *m_eth5TimeoutTimer = nullptr;


    QTimer *m_4gTimeoutTimer = nullptr;
    QTimer *m_usbTimeoutTimer = nullptr;

    void handleTestResult(
        bool& waitingFlag,
        bool& successFlag,
        QTimer* timeoutTimer,
        const QString& testName,
        const char* successKeyword,
        const char* failKeyword);

    void handleUserTestResult(
        bool& waitingFlag,
        bool& successFlag,
        QTimer* timeoutTimer,
        const QString& testName,
        const char* successKeyword,
        const char* failKeyword);

    void handleSimpleTestResult(
        bool& waitingFlag,
        bool& successFlag,
        QTimer* timeoutTimer,
        const QString& testName,
        const char* successKeyword,
        const char* failKeyword);



    QByteArray m_recvBuffer;

    // 新增：记录每个测试的最终结果
    bool m_ddrTestSuccess = false;
    bool m_emmcTestSuccess = false;
    bool m_rtcTestSuccess = false;
    bool m_wifiTestSuccess = false;
    bool m_output1TestSuccess = false;
    bool m_output2TestSuccess = false;
    bool m_input1TestSuccess = false;
    bool m_input2TestSuccess = false;
    bool m_422_1TestSuccess = false;
    bool m_422_2TestSuccess = false;

    // 485 测试最终结果
    bool m_485_1TestSuccess = false;
    bool m_485_2TestSuccess = false;
    bool m_485_3TestSuccess = false;
    bool m_485_4TestSuccess = false;

    bool m_eth1TestSuccess = false;
    bool m_eth2TestSuccess = false;
    bool m_eth3TestSuccess = false;
    bool m_eth4TestSuccess = false;
    bool m_eth5TestSuccess = false;

    bool m_4gTestSuccess = false;
    bool m_usbTestSuccess = false;


    // === 汇总报告专用 ===
    bool m_waitingForSummaryResult = false;
    QString m_summaryBuffer;
    QTimer* m_summaryTimeoutTimer = nullptr;

    // === 显示报告专用 ===
    bool m_waitingForShowReport = false;
    QString m_showReportBuffer;
    QTimer* m_showReportTimeoutTimer = nullptr;


    // mA 输出测试结果标志
    bool m_mAoutput1TestSuccess = false;
    bool m_mAoutput2TestSuccess = false;

    bool m_waitingFormaOutput1Result = false;
    bool m_waitingFormaOutput2Result = false;


    // 4-20mA 输入测试状态
    bool m_waitingFormAIn1Result = false;
    bool m_mAIn1TestSuccess = false;
    QTimer* m_mAIn1TimeoutTimer = nullptr;

    bool m_waitingFormAIn2Result = false;
    bool m_mAIn2TestSuccess = false;
    QTimer* m_mAIn2TimeoutTimer = nullptr;


    // HDD 测试状态
    bool m_waitingForHddResult = false;
    bool m_hddTestSuccess = false;
    QTimer* m_hddTimeoutTimer = nullptr;


    // 光口测试状态
    bool m_waitingForPbResult = false;
    bool m_pbTestSuccess = false;
    QTimer* m_pbTimeoutTimer = nullptr;

    // LED 测试状态
    bool m_waitingForLedResult = false;
    bool m_ledTestSuccess = false;




    // 在 private 中添加成员（用于管理新窗口）
    SecondWindow *m_secondWindow = nullptr;
    timer *m_timer = nullptr;
    FormDevInfo *m_devInfo = nullptr;
    FormDevLOG *m_devLog = nullptr;


    QDialog *m_waitDialog = nullptr;      // 浮窗指针
    QTimer *m_userTimeoutTimer = nullptr; // 用户等待超时（10秒）
    QLabel *m_waitLabel = nullptr;        // 显示倒计时的标签
    int m_waitSecondsLeft = 10;
    QTimer *m_countdownTimer = nullptr;
    int m_remainingSeconds = 10;


    //写入设备编号
    bool m_waitingForSnWrite = false;
    bool m_snWriteSuccess = false;
    QTimer* m_snWriteTimeoutTimer = nullptr;
    QString m_currentSnToWrite; // 用于校验返回的 SN 是否匹配（可选）

    //写入设备编号
        bool m_waitingForSn1Write = false;
        bool m_snWrite1Success = false;
        QTimer* m_snWrite1TimeoutTimer = nullptr;
        QString m_currentSn1ToWrite; // 用于校验返回的 SN 是否匹配（可选）
};

#endif // EXAMPLE_H
